package com.winchain.chainbill.bill.biz.discount.cmd;

import com.shell.bee.acct.account.po.AcctBas;
import com.shell.bee.base.exception.UnCheckMsgException;
import com.shell.bee.base.utils.MoneyUtil;
import com.shell.bee.base.utils.SystemUtil;
import com.shell.bee.contract.ContService;
import com.shell.bee.contract.ContTemplateHelper;
import com.shell.bee.contract.po.ContBas;
import com.shell.bee.contract.po.ContProt;
import com.shell.bee.contract.po.ContTmpl;
import com.shell.feign.PubProtocolFeign;
import com.shell.framework.command.CommandContext;
import com.shell.framework.command.ICommand;
import com.shell.framework.utils.CommonDataUtil;
import com.shell.framework.utils.SysDateUtil;
import com.shell.governor.modules.po.PubProtocol;
import com.winchain.chainbill.basic.charge.service.ValComService;
import com.winchain.chainbill.basic.charge.vo.ValComVo;
import com.winchain.chainbill.basic.cus.po.CusRec;
import com.winchain.chainbill.basic.cus.service.CusRecService;
import com.winchain.chainbill.bill.base.po.CbDct;
import com.winchain.chainbill.bill.base.po.CbRec;
import com.winchain.chainbill.bill.biz.Bill;
import com.winchain.chainbill.esign.service.ContBasSnService;
import com.winchain.chainbill.tbill.biz.contract.keyword.EsignDebitKeyword;
import com.winchain.chainbill.trade.service.BillAcctService;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BillSerFeeDebitSponsorCmd implements ICommand<Bill<CbDct>> {
	@Autowired
	private CusRecService cusRecService;
	@Autowired
	private ContBasSnService contBasSnService;
	@Autowired
	private PubProtocolFeign pubProtocolFeign;
	@Autowired
	private ContService contService;
	@Autowired
	private BillAcctService billAcctService;
	@Autowired
	private ValComService valComService;

	public boolean execute(CommandContext<Bill<CbDct>> context) throws Exception {
		CbRec cbRec = ((Bill) context.getSource()).getRec();
		CbDct cbDct = (CbDct) ((Bill) context.getSource()).getSource();
		if (cbDct == null) {
			return false;
		}
		if (!cbDct.getSellerId().equals(cbDct.getBuyerId())) {
			return false;
		}
		String bizId = cbDct.getBillId();
		String bizType = "18";
		String protTitle = "����������������";
		String sponsorId = CommonDataUtil.getCurEntId();

		CusRec artisan = this.cusRecService.getArtisan(null);
		if (artisan == null) {
			throw new UnCheckMsgException("��������������������������������");
		}
		String acceptorId = artisan.getCusId();
		String witness = cbDct.getFinId();
		String protocolType = "debit_proto";
		String grpId = cbRec.getGrpId();
		if (cbDct == null) {
			return false;
		}
		PubProtocol protocol = this.pubProtocolFeign.getPubProtocolByTypeAndGrpId(protocolType, grpId, null);
		List<ContTmpl> tmpls = this.contService.getContTmpl(bizType);
		List<ContProt> prots = null;
		CusRec cus = (CusRec) this.cusRecService.selectByKey(sponsorId);
		String contSn = this.contBasSnService.createContSn(cus.getGrpId());
		if ((protocol != null) && (tmpls != null) && (tmpls.size() > 0)) {
			prots = new ArrayList();
			String oriContent = protocol.getContent();
			String content = genContent(oriContent, cbDct, contSn, artisan, cbRec);
			ContTmpl tmpl = (ContTmpl) tmpls.get(0);
			ContProt prot = this.contService.genProtWithContent(tmpl.getTmplId(), protTitle, content, true);
			prots.add(prot);
		}
		ContBas cont = new ContBas();
		String contId = SystemUtil.getUUID();
		cont.setContId(contId);
		cont.setContSn(contSn);
		cont.setContTitle(protTitle);
		cont.setBizType(bizType);
		cont.setBizId(bizId);
		cont.setSponsor(sponsorId);
		cont.setAcceptor(acceptorId);
		cont.setWitness(witness);
		cont.setContDate(SysDateUtil.getSystemDate());
		cont.setCreateTime(SysDateUtil.getSystemDateTime());
		cont.setContState("1");
		cont.setContProgress("0");
		this.contService.saveCont(cont, prots);
		return false;
	}

	private String genContent(String content, CbDct source, String contSn, CusRec acceptor, CbRec rec) {
		EsignDebitKeyword keyword = new EsignDebitKeyword();
		keyword.setDebitContSn(contSn);
		keyword.setDebitDctAmt("��" + MoneyUtil.format(rec.getBillAmt()));
		keyword.setDebitBillCode(rec.getBillCode());
		keyword.setDebitWitness(source.getFinNm());

		BigDecimal serFee = source.getSerFee();

		ValComVo valComVo = this.valComService.getTbValComVo(source.getFinId(), rec.getGrpId());
		if (valComVo != null) {
			StringBuilder sb = new StringBuilder();
			String valType = valComVo.getValType();
			if (valComVo.getValStd() != null) {
				if ("1".equals(valType)) {
					sb.append(valComVo.getValStd().stripTrailingZeros().toPlainString()).append("��");
				} else if ("2".equals(valType)) {
					sb.append(valComVo.getValStd().stripTrailingZeros().toPlainString()).append("%");
				} else if ("3".equals(valType)) {
					sb.append(valComVo.getValStd().stripTrailingZeros().toPlainString()).append("��");
				}
			} else if ((sb != null) && (sb.length() > 0)) {
				sb.append("0%");
			}
			if ((sb != null) && (sb.length() > 0)) {
				keyword.setDebitSerFeeRate(sb.toString());
			}
		}
		keyword.setDebitSerFee("��" + MoneyUtil.format(serFee));
		if ((serFee == null) || (!MoneyUtil.biggerThanZero(serFee))) {
			keyword.setDebitSerFee("�� + 0");
		}
		CusRec sponsor = (CusRec) this.cusRecService.selectByKey(source.getSellerId());
		keyword.setDebitSponsor(sponsor.getCusName());
		keyword.setDebitSponsorLrNm(sponsor.getLrName());
		keyword.setDebitSponsorPhone(sponsor.getContactorPhone());
		AcctBas txAcct = this.billAcctService.getAcctBas(source.getSellerId(), null, "00");
		keyword.setDebitTxAcctNo(txAcct.getAcctNo());
		keyword.setDebitTxAcctNm(txAcct.getAcctNm());
		keyword.setDebitTxBankNm(txAcct.getFiNm());

		AcctBas cpAcct = this.billAcctService.getAcctBas(acceptor.getCusId(), null, "00");
		keyword.setDebitCpAcctNo(cpAcct.getAcctNo());
		keyword.setDebitCpAcctNm(cpAcct.getAcctNm());
		keyword.setDebitCpBankNm(cpAcct.getFiNm());
		keyword.setDebitAcceptor(acceptor.getCusName());
		keyword.setDebitAcceptorLrNm(acceptor.getLrName());
		keyword.setDebitAcceptorPhone(acceptor.getContactorPhone());

		String signDate = SysDateUtil.parseDate(SysDateUtil.getSystemDate(), "yyyyMMdd", "yyyy��MM��dd��");
		keyword.setDebitSponsorSignDate(signDate);
		keyword.setDebitAcceptorSignDate(signDate);
		keyword.setDebitWitnessSignDate(signDate);
		return ContTemplateHelper.replaceContent(content, keyword);
	}
}
